package com.yantong.controller;


import com.yantong.service.CentreService;
import com.yantong.annotation.Log;
import com.yantong.constant.BusinessCode;
import com.yantong.constant.PermisFlag;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.SysRole;
import com.yantong.service.RoleService;
import com.yantong.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author kanghaixin
 */
@RequestMapping("/sys/role")
@Controller
public class RoleController  extends BaseController{
    private static final String prefix = "system/role";
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
	private CentreService centreService;

    /**
     * 角色配置界面
     *
     * @return
     */
    @RequiresPermissions("sys:role:role")
    @GetMapping()
    public String role(Model model) {
    	model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/role";
    }


    /**
     * 获取所有的角色
     *
     * @return
     */
    @RequiresPermissions("sys:role:role")
    @GetMapping("/list")
    @ResponseBody()
    public PageUtils<SysRole> list(Long centreId ,Integer offset, Integer limit) {
        HashMap<String,Object> params = new HashMap<>(2);
        if(centreId==null) {
        	if (PermisFlag.FLAG_CENTRE.equals( getUserFlag())) {
                params.put("centreId", getUserCentreId());
            }
        }else {
        	 params.put("centreId", centreId);
        }
        return roleService.listRoles(params, offset, limit);
    }

    /**
     * 角色新增页面
     *
     * @return
     */
    @RequiresPermissions("sys:role:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 角色编辑页面
     *
     * @param roleId
     * @param model
     * @return
     */
    @RequiresPermissions("sys:role:edit")
    @GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, Model model) {
        SysRole sysRole = roleService.getRoleByKey(roleId);
        model.addAttribute("role", sysRole);
        return prefix + "/edit";
    }


    /**
     * 角色新增
     *
     * @param role
     * @return
     */
    @Log(module = "角色管理", operation = "新增")
    @RequiresPermissions("sys:role:add")
    @PostMapping("/save")
    @ResponseBody()
    public ResultVo save(SysRole role) {
        role.setCentreId(getUserCentreId());
        if (roleService.saveRole(role) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.ROLE_SAVE_ERROR);
        }
    }


    /**
     * 角色编辑
     *
     * @param role
     * @return
     */
    @Log(module = "角色管理", operation = "编辑")
    @RequiresPermissions("sys:role:edit")
    @PostMapping("/update")
    @ResponseBody()
    public ResultVo update(SysRole role) {
        if (roleService.updateRole(role) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.ROLE_UPDATE_ERROR);
        }
    }


    /**
     * 角色删除
     *
     * @param roleId
     * @return
     */
    @Log(module = "角色管理", operation = "删除")
    @RequiresPermissions("sys:role:remove")
    @PostMapping("/remove")
    @ResponseBody()
    public ResultVo remove(Long roleId) {
        if (roleId == null) {
            return ResultVo.error();
        }
        if(roleId < 10){
            return ResultVo.error(BusinessCode.ROLE_DELETE_ILLEGAL);
        }
        if (roleService.removeRole(roleId) > 0) {
            return ResultVo.ok();
        } else {
            return ResultVo.error(BusinessCode.ROLE_DELETE_ERROR);
        }
    }

    /**
     * 角色批量删除
     *
     * @param ids
     * @return
     */
    @Log(module = "角色管理", operation = "批量删除")
    @RequiresPermissions("sys:role:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public ResultVo batchRemove(@RequestParam("ids[]") Long[] ids) {
        if (ids.length < 1) {
            return ResultVo.ok();
        }
        if (roleService.batchRemove(ids) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }
}
